package java_thinking.unit_14._14_6_reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019-03-30 15:42
 */

 class ShowMethods {
    private static String usage =
            "usage:\n ShowMethods qualified.class.name\n" +
                    "To show all method in class or:\n" +
                    "ShowMethods qualified.class.name word\n" +
                    "To search for methods involving 'word'";
    /**
     * \\. 单一的匹配.
     * .匹配除换行符 \n 之外的任何单字符
     */
    private static Pattern p = Pattern.compile("\\w+\\.");
    private static Pattern p1 = Pattern.compile("native|final");

    public static void main(String[] args) {
        String[] params = {"java_thinking.unit_14._14_6_reflect.ShowMethods"};
        if (params.length < 1){
            System.out.println(usage);
            System.exit(0);
        }
        int lines = 0;
        try {
            //Class.forName 生成的结果在编译时是不可知的，因此所有的方法特征签名信息都是在执行时被提取出来的
            Class<?> c = Class.forName(params[0]);
            //父类的方法也会一起获取
            Method[] methods = c.getMethods();
            //只获取了自己的构造方法
            Constructor[] ctors = c.getConstructors();
            if (params.length == 1){
                for (Method method : methods){
                    System.out.println(p1.matcher(method.toString()).replaceAll(""));
                }
                for (Constructor constructor:ctors){
                    System.out.println(p1.matcher(constructor.toString()).replaceAll(""));
                }
                lines = methods.length + ctors.length;
            } else {
                for (Method method:methods){
                    if (method.toString().indexOf(params[1]) != -1){
                        System.out.println(p.matcher(method.toString()).replaceAll(""));
                        lines++;
                    }
                }
                for (Constructor constructor:ctors){
                    if (constructor.toString().indexOf(params[1]) != -1){
                        System.out.println(p.matcher(constructor.toString()).replaceAll(""));
                        lines++;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            System.out.println("No such class");
        }
    }
}

class TestMech{
    private static Pattern p = Pattern.compile("\\w+\\.");

    public static void main(String[] args) {
        String s = "public static void java_thinking.unit_14._14_6_reflect.ShowMethods.maindemo(java.lang.String[])";
        System.out.println(s);
        System.out.println(p.matcher(s).replaceAll("-"));
    }
}
/*
public static void maindemo(String[])
public final void wait() throws InterruptedException
public final void wait(long,int) throws InterruptedException
public final native void wait(long) throws InterruptedException
public boolean equals(Object)
public String toString()
public native int hashCode()
public final native Class getClass()
public final native void notify()
public final native void notifyAll()
默认构造器，可以在上面代码中看到根本没有定义任何构造器，所以是编译器自动合成的
如果将ShowMethods作为一个非public 的类，也就是拥有包访问权限，输出中就不会
再显示出这个自动合成的默认构造器了！！！
public ShowMethods()
* */